'''
user.py 存储用户注册，用户登录相关操作
'''

from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get('http://39.107.96.138:3000/')

driver.maximize_window()
driver.implicitly_wait(15)

def find_xpath(xpath):
    return driver.find_element_by_xpath(xpath)

def find_css(css):
    return driver.find_element_by_css_selector(css)
'''
flag 只有2个值，成功或失败，表示期望运行结果的一个状态
flag == 成功  用户登录成功的场景
flag == 失败  用户登录失败的场景
'''

def user_login(username,passwd,flag):
    find_xpath('//*[@class="nav pull-right"]//a[@href="/signin"]').click()
    find_css('#name').clear()
    find_css('#name').send_keys(username)
    find_css('#pass').clear()
    find_css('#pass').send_keys(passwd)
    find_xpath('//*[@type="submit"]').click()

    if flag == '成功':
        result_username = find_xpath('//*[@class="user_name"]//a[@class="dark"]').text
        return result_username

    elif flag == '失败':
        result_text = find_xpath('//strong').text
        return result_text

# username = ''
# passwd = ''
# flag = '失败'
#
# user_login(username,passwd,flag)
# user_login('testuser1','123456','成功')
# user_login('testuser1','12346','失败')
# user_login('','','失败')

def user_registe(username,passwd,re_passwd,email,flag):
    find_xpath('//*[@class="nav pull-right"]//a[@href="/signup"]').click()
    find_css('#loginname').clear()
    find_css('#loginname').send_keys(username)
    find_css('#pass').clear()
    find_css('#pass').send_keys(passwd)
    find_css('#re_pass').clear()
    find_css('#re_pass').send_keys(re_passwd)
    find_css('#email').clear()
    find_css('#email').send_keys(email)
    find_xpath('//*[@type="submit"]').click()

    if flag == '成功':
        pass
    elif flag == '失败':
        result_text = find_xpath('//strong').text
        return result_text



if __name__ == '__main__':
    try:
        user_login("testuser1","123456","失败")
    except Exception as e:
        print("Exception",e)

    user_registe()